exports.domark = async (context) => {
    const cloud = context.cloud;
    const { openId } = context
    const { shopId, itemId, running_id } = context.data
    if (!itemId || !shopId || !running_id) {
        //校验参数
        return { success: false, data: "", message: "请传入正确的参数" }
    }
    try {
        //查询数据库商家的session信息
        const result = await cloud.db.collection('c_key').find({
            name: 'session',
        });
        if (result && result.length > 0) {
            //查询到相关session
            const shopSession = result[0][shopId]
            if (shopSession) {
                //查询到相关session，则调用打标接口
                const apiResult = await context.cloud.topApi.invoke({
                    api: 'taobao.opentrade.special.users.mark',
                    data: {
                        'hit': true,
                        'open_user_ids': openId,
                        'item_id': itemId,
                        'sku_id': "0",
                        'status': 'MARK',
                        'limit_num': 1,
                        'session': shopSession
                    },
                });
                if (apiResult && apiResult.result && apiResult.result > 0) {
                    //打标成功，则先将打标信息存入记录表
                    try {
                        await cloud.db.collection('mark_record').insertOne({
                            openId,
                            shopId,
                            itemId,
                            running_id,
                            createTime: Date.now(),
                        });
                    } catch (error2) {
                        console.log('新增失败', error2)
                    }
                    //打标成功
                    return { success: true, data: apiResult, message: "请求成功" };
                }
            }
            return { success: false, data: "", message: "系统错误" }
        }
        return { success: false, data: "", message: "系统错误" }
    } catch (error) {
        return { success: false, data: error, message: "系统错误" }
    }
};


exports.getUrl = async (context) => {
    const cloud = context.cloud;
    const { running_id } = context.data
    if (!running_id) {
        //校验参数
        return { success: false, data: "", message: "请传入正确的参数" }
    }
    const urlobj = {
      "10002":"https://m.duanqu.com/?_ariver_appid=3000000053747493&_mp_code=tb&transition=present",
      "10003":"https://m.duanqu.com?_ariver_appid=3000000048426063&nbsv=2.4.77&nbsource=debug&nbsn=TRIAL&_mp_code=tb&transition=present",
      "10014":"https://m.duanqu.com/?_ariver_appid=3000000053749622&_mp_code=tb&transition=present"
    }
    try {
        //查询数据库商家的session信息
        const result = await cloud.db.collection('c_key').find({
            name: 'url',
            running_id,
        });
        if(result.length>0) {
          return { success: true, data: {
            url:result[0],
            openId:context.openId,
            currentTime: Date.now()
          }, message: "请求成功" }
        }
        return { success: true, data: {
            url:urlobj,
            openId:context.openId,
            currentTime: Date.now()
          }, message: "请求成功" }
    } catch (error) {
        return { success: false, data: error, message: "系统错误" }
    }
};

// exports.domarkReissue = async (context) => {
//     const cloud = context.cloud;
//     const { shopId, itemId, openId } = context.data
//     if (!itemId || !shopId || !openId) {
//         //校验参数
//         return { success: false, data: "", message: "请传入正确的参数" }
//     }
//     try {
//         //查询数据库商家的session信息
//         const result = await cloud.db.collection('c_key').find({
//             name: 'session',
//         });
//         if (result && result.length > 0) {
//             //查询到相关session
//             const shopSession = result[0][shopId]
//             if (shopSession) {
//                 //查询到相关session，则调用打标接口
//                 const apiResult = await context.cloud.topApi.invoke({
//                     api: 'taobao.opentrade.special.users.mark',
//                     data: {
//                         'hit': true,
//                         'open_user_ids': openId,
//                         'item_id': itemId,
//                         'sku_id': "0",
//                         'status': 'MARK',
//                         'limit_num': 1,
//                         'session': shopSession
//                     },
//                 });
//                 if (apiResult && apiResult.result && apiResult.result > 0) {
//                     //打标成功，则先将打标信息存入记录表
//                     try {
//                         await cloud.db.collection('mark_record_reissue').insertOne({
//                             openId,
//                             shopId,
//                             itemId,
//                             createTime: Date.now(),
//                         });
//                     } catch (error2) {
//                         console.log('新增失败', error2)
//                     }
//                     //打标成功
//                     return { success: true, data: apiResult, message: "请求成功" };
//                 }
//             }
//             return { success: false, data: "", message: "系统错误" }
//         }
//         return { success: false, data: "", message: "系统错误" }
//     } catch (error) {
//         return { success: false, data: error, message: "系统错误" }
//     }
// };